<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button id="add">add hash</button>
  <button id="update">update hash</button>

  <script>

    document.querySelector('#add').addEventListener('click', () => {
      location.hash = "abc"
    })

    document.querySelector('#update').addEventListener('click', () => {
      location.hash = "efg"
    })

    addEventListener('hashchange', () => {
      console.log(location.hash)
    })

  </script>

</body>

</html>